package com.theo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.CommentInfo;
import com.theo.entity.TopicInfo;
import com.theo.entity.TypeInfo;
import com.theo.entity.UserInfo;
import com.theo.service.CommentInfoService;
import com.theo.service.TopicInfoService;
import com.theo.service.TypeInfoService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/topic-info")
public class TopicInfoController {
    @Autowired
    private TopicInfoService topicInfoService;
    @Autowired
    private TypeInfoService typeInfoService;
    @Autowired
    private CommentInfoService commentInfoService;

    @RequestMapping("/showList")
    public String showList(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<TopicInfo> topicInfos = topicInfoService.queryAllTopicInfoByPage();
        PageInfo pageInfo = new PageInfo(topicInfos, 2);
        model.addAttribute("pageInfo", pageInfo);
        List<TypeInfo> topicTypeList = typeInfoService.queryTopicTypeInfo();
        model.addAttribute("categoryList", topicTypeList);
        return "user/topic_list";
    }

    @RequestMapping("/showListType")
    public String queryAllByTopicTypeId(String id,Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<TopicInfo> topicInfos = topicInfoService.queryAllByTopicTypeId(id);
        PageInfo pageInfo = new PageInfo(topicInfos, 2);
        model.addAttribute("pageInfo", pageInfo);
        List<TypeInfo> topicTypeList = typeInfoService.queryTopicTypeInfo();
        model.addAttribute("categoryList", topicTypeList);
        return "user/topic_list";
    }
    @RequestMapping("/show.do")
    public String show(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<TopicInfo> topicInfos = topicInfoService.queryAllTopicInfoByPage();
        PageInfo pageInfo = new PageInfo(topicInfos, 2);
        model.addAttribute("pageInfo", pageInfo);
        List<TypeInfo> topicTypeList = typeInfoService.queryTopicTypeInfo();
        model.addAttribute("categoryList", topicTypeList);
        return "admin/topic_list";
    }

    @RequestMapping("/toInput.do")
    public String toWriteTopic(Model model) {
        List<TypeInfo> topicTypeList = typeInfoService.queryTopicTypeInfo();
        model.addAttribute("topicTypeList", topicTypeList);
        return "admin/topic_input";
    }

    @RequestMapping("/input.do")
    public String writeTopic(TopicInfo topicInfo) {
        topicInfoService.saveTopicInfo(topicInfo);
        return "redirect:/topic-info/show.do";
    }

    @RequestMapping("/deleteSelf.do")
    public String deleteTopicSelf(Integer id) {
        topicInfoService.deleteTopicInfo(id);
        return "redirect:/topic-info/showList";
    }

    @RequestMapping("/delete.do")
    public String deleteTopic(Integer id) {
        topicInfoService.deleteTopicInfo(id);
        return "redirect:/topic-info/show.do";
    }

    @RequestMapping("/read.do")
    public String readTopic(Integer id, Model model) {
        TopicInfo topicInfo = topicInfoService.queryTopicInfoById(id);
        topicInfo.setTopicViews(topicInfo.getTopicViews() + 1);
        topicInfoService.updateTopicInfo(topicInfo);
        model.addAttribute("topicInfo", topicInfo);
        // 查询评论
        List<CommentInfo> commentList = commentInfoService.queryCommentInfo(id);
        model.addAttribute("commentList", commentList);
        return "user/topic_one";
    }

    @RequestMapping("/writeComment.do")
    public String writeComment(CommentInfo commentInfo) {
        commentInfoService.saveCommentInfo(commentInfo);
        return "redirect:/topic-info/read.do?id=" + commentInfo.getTopicId();
    }
    @RequestMapping("/deleteComment.do")
    public String deleteComment(Integer id) {
        CommentInfo commentInfo = commentInfoService.queryCommentInfoById(id);
        commentInfoService.deleteCommentInfo(id);
        return "redirect:/topic-info/read.do?id=" + commentInfo.getTopicId();
    }
}

